package com.zbbt.sportslottery.ui.activity;

import com.umeng.fb.FeedbackAgent;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.app.UpdateVersion;
import com.zbbt.sportslottery.util.LogUtil;
import com.zbbt.sportslottery.util.SharePreferUtil;
import com.zbbt.sportslottery.util.ToastUtil;
import com.zbbt.sportslottery.util.UmengUtil;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SettingsActicity extends SimpleBaseActivity {

	private RelativeLayout exitOrlogin;
	private LinearLayout qq;
	private RelativeLayout email;
	private LinearLayout about;
	private TextView logText;
	private LinearLayout update;
	private UpdateVersion updateVersion;
	private RelativeLayout feedback;
	private RelativeLayout twocode;
	private RelativeLayout grade;
	private LinearLayout changePsd;
	private TextView exit_tv;
	private static Handler handler;
	private TextView recommend;
	private RelativeLayout exit_relative;

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
		setContentView(R.layout.fragment_settings);
	}

	@Override
	protected void initView() {
		qq = (LinearLayout) findViewById(R.id.qq);
		email = (RelativeLayout) findViewById(R.id.email);
		about = (LinearLayout) findViewById(R.id.about);
		exitOrlogin = (RelativeLayout) findViewById(R.id.exit_login);
		update = (LinearLayout) findViewById(R.id.update);
		logText = (TextView) findViewById(R.id.log_exit);
		feedback = (RelativeLayout) findViewById(R.id.feedback);
		grade = (RelativeLayout) findViewById(R.id.grade);
		// twocode = (RelativeLayout) findViewById(R.id.two_code);
		changePsd = (LinearLayout) findViewById(R.id.change_password);
		exit_relative = (RelativeLayout) findViewById(R.id.exit_relative);
		exit_tv = (TextView) findViewById(R.id.exit_tv);
		recommend = (TextView) findViewById(R.id.recommend);
	}

	@Override
	protected void setListener() {
		qq.setOnClickListener(this);
		email.setOnClickListener(this);
		about.setOnClickListener(this);
		exitOrlogin.setOnClickListener(this);
		update.setOnClickListener(this);
		feedback.setOnClickListener(this);
		// twocode.setOnClickListener(this);
		changePsd.setOnClickListener(this);
		grade.setOnClickListener(this);
		exit_tv.setOnClickListener(this);
		recommend.setOnClickListener(this);
	}

	@Override
	protected void onClickEvent(View v) {
		switch (v.getId()) {
		case R.id.qq:
			copy(context.getResources().getString(R.string.qqnum));
			UmengUtil.onEvent(context, Constant.UMENG_SET_QQ);
			break;
		case R.id.email:
			contantUs(context);
			UmengUtil.onEvent(context, Constant.UMENG_SET_EMAIL);
			break;
		case R.id.about:
			goActivity(AboutUsActivity.class);
			UmengUtil.onEvent(context, Constant.UMENG_SET_ABOUT);
			break;
		case R.id.feedback:
			// goActivity(FeedBackActivity.class);
			// FeedbackAgent agent=new FeedbackAgent(this);
			// agent.startFeedbackActivity();
			goActivity(CustomActivity.class);
			UmengUtil.onEvent(context, Constant.UMENG_SET_OPINON);
			break;
		case R.id.exit_login:
			// 发送广播。。改变个人信息
			if (App.isLogin) {
				Intent intent = new Intent(Constant.INTENT_ACTION_CHANGE_LOGIN);
				context.sendBroadcast(intent);
			} else {
				logText.setText("登录");
			}
			UmengUtil.onEvent(context, Constant.UMENG_SET_LOGIN);
			goActivity(LoginActivity.class);
			break;
		case R.id.update:
			// 检测版本更新
			LogUtil.printD(TAG, "updateVersion");
			updateVersion = new UpdateVersion(context);
			updateVersion.requestUpdateData(UpdateVersion.PAGE_SETTING);
			UmengUtil.onEvent(context, Constant.UMENG_SET_UPDATE);
			break;
		// case R.id.two_code:
		// goActivity(TwoCodeActivity.class);
		// break;
		case R.id.change_password:
			UmengUtil.onEvent(context, Constant.UMENG_SET_MODIFI);
			goActivity(ModifiPwdActivity.class);
			break;
		case R.id.grade:
			UmengUtil.onEvent(context, Constant.UMENG_SET_GRADE);
			try {
				Uri uri = Uri.parse("market://details?id=" + getPackageName());
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(Intent.createChooser(intent, "请选择应用市场"));
			} catch (Exception e) {
				LogUtil.printI("dawdafrf", e.toString());
			}

			break;
		case R.id.exit_tv:
			UmengUtil.onEvent(context, Constant.UMENG_SET_EXIT);
			SharePreferUtil.putString(Constant.PASSWORD, "");
			SharePreferUtil.putString(Constant.USER_PHOTO_IMG, "");
			App.isLogin = false;
			new Thread(new Runnable() {
				@Override
				public void run() {
					Message message = new Message();
					message.what = Constant.INTENT_LOGIN;
					handler.sendMessage(message);
				}
			}).start();
			ToastUtil.showLong("退出登陆成功");
			goActivity(LoginActivity.class);
			// changePsd.setVisibility(View.GONE);
			// exit_relative.setVisibility(View.GONE);
			break;
		case R.id.recommend:
			UmengUtil.onEvent(context, Constant.UMENG_SET_REMOND);
			goActivity(RecomActivity.class);
			break;
		default:
			break;
		}
	}

	public void contantUs(Context mContext) {
		try {
			Intent emailIntent = new Intent(Intent.ACTION_SEND);
			emailIntent.setType("message/rfc822");
			emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "kf@lottnews.com" });
			mContext.startActivity(emailIntent);
		} catch (Exception e) {
			ToastUtil.showLong("请设置本机邮箱账号！");
			e.printStackTrace();
		}

	}

	private void copy(String dstString) {
		// 得到剪贴板管理器
		ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
		cmb.setText(dstString);
		ToastUtil.showLong(context.getResources().getString(R.string.copy_success));
	}

	@Override
	protected void initData() {

	}

	public static void setHandler(Handler handler1) {
		handler = handler1;
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (App.isLogin) {
			logText.setText("切换帐号");
			changePsd.setVisibility(View.VISIBLE);
			exit_relative.setVisibility(View.VISIBLE);
		} else {
			logText.setText("登录");
			changePsd.setVisibility(View.GONE);
			exit_relative.setVisibility(View.GONE);
		}

	}

}
